package com.cjjd.c.base;

import android.app.Dialog;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.cjjd.c.R;
import com.cjjd.c.http.RetrofitInterface;
import com.cjjd.c.http.RetrofitSingleton;
import com.cjjd.c.view.ActionBarView;
import com.cjjd.c.view.WheelView_add;

import java.util.ArrayList;

/**
 * ----------------------------------
 * Created by xyd on 2017/3/15.
 * <p/>
 * 描  述：
 * <p/>
 * 修订历史:
 * ----------------------------------
 */
public abstract class ActionBarFragment extends BaseFragment implements ActionBarView.ActionBarListener, View.OnTouchListener {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return true;
    }

    /**
     * 标题栏
     */
    protected ActionBarView mActionBar;
    public RetrofitInterface service;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_base, container, false);
        CoordinatorLayout frameLayout = (CoordinatorLayout) root.findViewById(R.id.fragment_body);
        if (getContentView() != 0) {
            View contentView = inflater.inflate(getContentView(), root, false);
            frameLayout.addView(contentView);
        }

        mActionBar = (ActionBarView) root.findViewById(R.id.actionbar_title);
        mActionBar.setTitle(getTitleString());
        //左键监听
        mActionBar.getLeftButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onLeftButtonClick();
            }
        });
        //右键监听
        mActionBar.getRightButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onRightButtonClick();
            }
        });

        initLeftButton(mActionBar.getLeftButton());
        initRightButton(mActionBar.getRightButton());

        service = RetrofitSingleton.getService(mContext);
        root.setOnTouchListener(this);
        return root;
    }

    protected void initView() {
    }

    ;

    // =================↓ actionBar==================
    @Override
    public void onLeftButtonClick() {
        mContext.onBackPressed();
    }

    @Override
    public void onRightButtonClick() {

    }

    @Override
    public void initLeftButton(Button leftButton) {

    }

    @Override
    public void initRightButton(Button rightButton) {

    }

    public void setTitle(Object obj) {
        mActionBar.setTitle(obj);
    }

    // =================↑ actionBar ==================

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }


    private Dialog multiChooseSingeDialog;

    protected void showMutiChooseDateDialog(final ArrayList<String> provice, final ArrayList<String> city, final ArrayList<String> region,
                                            final String title) {
        View view = mContext.getLayoutInflater().inflate(R.layout.three_wheelview_dialog, null);
        multiChooseSingeDialog = new Dialog(mContext, R.style.transparentFrameWindowStyle);
        multiChooseSingeDialog.setContentView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        Window window = multiChooseSingeDialog.getWindow();
        // 设置显示动画
        window.setWindowAnimations(R.style.main_menu_animstyle);
        WindowManager.LayoutParams wl = window.getAttributes();
        wl.x = 0;
        wl.y = mContext.getWindowManager().getDefaultDisplay().getHeight();
        // 以下这两句是为了保证按钮可以水平满屏
        wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
        wl.height = ViewGroup.LayoutParams.WRAP_CONTENT;

        // 设置显示位置
        multiChooseSingeDialog.onWindowAttributesChanged(wl);
        TextView tv_title = (TextView) view.findViewById(R.id.title);
        tv_title.setText(title);
        final WheelView_add wheelView_province = (WheelView_add) view.findViewById(R.id.province);
        final WheelView_add wheelView_city = (WheelView_add) view.findViewById(R.id.city);
        final WheelView_add wheelView_region = (WheelView_add) view.findViewById(R.id.region);

        if (provice != null && provice.size() != 0
                && city != null && city.size() != 0
                && region != null && region.size() != 0) {
            wheelView_province.setData(provice);
            wheelView_city.setData(city);
            wheelView_region.setData(region);
        } else {
            // ToastUtil.showShortToast(mContext, "无可用数据");
        }

        //wheelView_province.setItemNumber(5);
        //wheelView_city.setItemNumber(5);
        //wheelView_region.setItemNumber(5);

        wheelView_province.setOnSelectListener(new WheelView_add.OnSelectListener() {
            @Override
            public void endSelect(int id, String text) {
                firstWheelviewChange(id, text, wheelView_city,wheelView_region);
            }

            @Override
            public void selecting(int id, String text) {
                //getChangeDialogItem(id, text);
            }
        });

        wheelView_city.setOnSelectListener(new WheelView_add.OnSelectListener() {
            @Override
            public void endSelect(int id, String text) {
                secondWheelviewChange(id, text, wheelView_region);
            }

            @Override
            public void selecting(int id, String text) {
                //getChangeDialogItem(id, text);
            }
        });

        Button btn = (Button) view.findViewById(R.id.wheelview_dialog_btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                getMultiResult(wheelView_province.getSelected(),wheelView_city.getSelected(),wheelView_region.getSelected(),title);
                //getChooseDialog(wheelView.getSelected(), date.get(wheelView.getSelected()), title);
                multiChooseSingeDialog.dismiss();
            }
        });
        // 设置点击外围解散
        multiChooseSingeDialog.setCanceledOnTouchOutside(true);


        if (provice != null && provice.size() != 0
                && city != null && city.size() != 0
                && region != null && region.size() != 0) {
            multiChooseSingeDialog.show();
        }
    }

    /**
     * 获取三级联动的结果。
     * @param selected0
     * @param selected1
     * @param selected2
     * @param title
     */
    protected void getMultiResult(int selected0, int selected1, int selected2, String title) {
    }

    /**
     * 第一个联动滑动器发生滑动
     * @param id
     * @param text
     * @param wheelView_second
     * @param wheelView_third
     */
    protected void firstWheelviewChange(int id, String text, WheelView_add wheelView_second, WheelView_add wheelView_third) {

    }
/**
     * 第二个联动滑动器发生滑动
     * @param id
     * @param text
     * @param wheelView_third
     */
    protected void secondWheelviewChange(int id, String text, WheelView_add wheelView_third) {

    }


    private Dialog chooseSingeDialog;

   protected void showChooseDateDialog(final ArrayList<String> date, final String title) {
        View view = mContext.getLayoutInflater().inflate(R.layout.wheelview_dialog, null);
        chooseSingeDialog = new Dialog(mContext, R.style.transparentFrameWindowStyle);
        chooseSingeDialog.setContentView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        Window window = chooseSingeDialog.getWindow();
        // 设置显示动画
        window.setWindowAnimations(R.style.main_menu_animstyle);
        WindowManager.LayoutParams wl = window.getAttributes();
        wl.x = 0;
        wl.y = mContext.getWindowManager().getDefaultDisplay().getHeight();
        // 以下这两句是为了保证按钮可以水平满屏
        wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
        wl.height = ViewGroup.LayoutParams.WRAP_CONTENT;

        // 设置显示位置
        chooseSingeDialog.onWindowAttributesChanged(wl);
        TextView tv_title = (TextView) view.findViewById(R.id.title);
        tv_title.setText(title);
        final WheelView_add wheelView = (WheelView_add) view.findViewById(R.id.wheelview_single);
        if (date != null && date.size() != 0) {
            wheelView.setData(date);
        } else {
//            ToastUtil.showShortToast(mContext, "无可用数据");
        }
        wheelView.setItemNumber(5);
        wheelView.setOnSelectListener(new WheelView_add.OnSelectListener() {
            @Override
            public void endSelect(int id, String text) {

            }

            @Override
            public void selecting(int id, String text) {
                getChangeDialogItem(id, text);
            }
        });

        Button btn = (Button) view.findViewById(R.id.wheelview_dialog_btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                getChooseDialog(wheelView.getSelected(), date.get(wheelView.getSelected()), title);
                chooseSingeDialog.dismiss();
            }
        });
        // 设置点击外围解散
        chooseSingeDialog.setCanceledOnTouchOutside(true);
        if (date != null && date.size() != 0) {
            chooseSingeDialog.show();
        }
    }

    /**
     * 滑动的时候，返回列表的itemId，和该项的字段
     *
     * @param index
     * @param s
     */
    protected void getChangeDialogItem(int index, String s) {
    }

    /**
     * 确定选择的时候，返回列表的index，选中的内容，和title
     *
     * @param index
     * @param message
     * @param title
     */
    protected void getChooseDialog(int index, String message, String title) {

    }

}